﻿using DevExpress.XtraRichEdit.Import.Html;
using HalconDotNet;
using ImageCalibModule;
using ImageDefine;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ImageToolTransfer
{
    public class ImageActionerTransfer
    {
        public ImageToolTransfer imageTool;

        public ImageActionerTransfer()
        {

        }
        public ImageActionerTransfer(ImageToolTransfer toolBase)
        {
            imageTool = toolBase;

        }

        public bool Run(HalconDotNet.HImage imageInput, SmartDisplayControl.SmartDisplayControl disPlayControl)
        {
            bool bResult = false;
            GeneralLib.HiPerfTimer timer = new GeneralLib.HiPerfTimer();
            timer.Start();

            try
            {
                imageTool.ImageToolResults = new List<ImageToolResult>();
                imageTool.OutPutResults.Clear();
                if (imageTool.strPositionName == "" || imageTool.strAngleName == "")
                {
                    bResult = false;
                    imageTool.bResultOK = false;
                }
                else
                {
                    try
                    {

                        ImageToolBase toolFrom = imageTool.imageTask.dicTools[imageTool.strPositionName];
                        ImageToolBase toolTo = imageTool.imageTask.dicTools[imageTool.strAngleName];
                        if ((toolFrom.ImageToolResults.Count>0))// == toolTo.ImageToolResults.Count) && toolFrom.ImageToolResults.Count > 0)
                        {
                            for (int i = 0; i < toolFrom.ImageToolResults.Count; i++)
                            {
                                RunToool(imageInput, disPlayControl, i, toolFrom, toolTo);
                                //RunToool(imageInput, disPlayControl, i, toolFrom, null);
                            }
                            ImageResultLine imageResultCircle = imageTool.ImageToolResults[0] as ImageResultLine;
                            imageTool.ResultX = imageResultCircle.ResultX;
                            imageTool.ResultY = imageResultCircle.ResultY;
                            imageTool.dResultRow1 = imageResultCircle.LineStartY;
                            imageTool.dResultCol1 = imageResultCircle.LineStartX;
                            imageTool.dResultRow2 = imageResultCircle.LineEndY;
                            imageTool.dResultCol2 = imageResultCircle.LineEndX;
                            imageTool.bResultOK = imageResultCircle.Result;
                            imageTool.dResultA = imageResultCircle.ResultA;
                        }
                        else
                        {
                            bResult = false;
                            imageTool.bResultOK = false;
                        }

                    }
                    catch
                    {

                    }
                }
            }
            catch//(Exception ex)
            {
                bResult = false;
                imageTool.bResultOK = false;
            }

            imageTool.dTimeUsed = timer.Duration;
            return bResult;
        }
        public bool RunToool(HImage image, SmartDisplayControl.SmartDisplayControl disWnd, int iIndex, ImageToolBase toolFrom, ImageToolBase toolTo)
        {
            imageTool.ResultX = 0.0;
            imageTool.ResultY = 0.0;
            imageTool.ResultA = 0.0;
            imageTool.ResultOK = false;
            imageTool.dResultScore = 0.0;
            RunPointAndLine(image, disWnd, iIndex, toolFrom, toolTo);
            return true;

        }
       
        

        public void RunPointAndLine(HImage image, SmartDisplayControl.SmartDisplayControl disWnd, int iIndex, ImageToolBase toolFrom, ImageToolBase toolTo)
        {
            IPoint pointFrom = toolFrom as IPoint;
            ILineTool pointTo = toolTo as ILineTool;
            ImageResultLine imageResultLine = new ImageResultLine();
            if (pointFrom != null && pointTo != null)
            {
                double rowPoint, columnPoint, row1, column1, row2, column2, projectionRow, projectionColumn;

                bool bResultPointFrom, bResultPointTo;
                bResultPointFrom = pointFrom.GetPoint(out columnPoint, out rowPoint, iIndex);
                bResultPointTo = pointTo.GetLine(out row1, out column1, out row2, out column2, iIndex);
                if (bResultPointFrom&& bResultPointTo)
                {
                    ImageCalibModule.ImageCalibHandeye imageCalibHandeye = ImageCalibModule.ImageCalibManager.ImageCalibHandeye;
                    
                    double dCenterTransX, dCenterTransY;
                    double dLineTransX1, dLineTransY1;
                    double dLineTransX2, dLineTransY2;
  
                    imageCalibHandeye.TramImageToWorld(columnPoint, rowPoint, 0.0, 0.0, out dCenterTransX, out dCenterTransY);
                    imageCalibHandeye.TramImageToWorld(column1, row1, 0.0, 0.0, out dLineTransX1, out dLineTransY1);
                    imageCalibHandeye.TramImageToWorld(column2, row2, 0.0, 0.0, out dLineTransX2, out dLineTransY2);
                    if (imageTool.Setting.角度置零)
                    {
                        imageResultLine.ResultA = 0;
                    }   
                    else
                    {
                        imageResultLine.ResultA=MakeAngleToAngle180(dLineTransY1, dLineTransX1, dLineTransY2, dLineTransX2);
                    }
                    imageResultLine.ResultX = dCenterTransX;
                    imageResultLine.ResultY = dCenterTransY;
                    imageResultLine.ResultScore = 1;
                    imageResultLine.LineStartX =  dLineTransX1;
                    imageResultLine.LineStartY =  dLineTransY1;
                    imageResultLine.LineEndX =  dLineTransX2;
                    imageResultLine.LineEndY =  dLineTransY2;
                    imageResultLine.Result = true;
                  

                }
                else
                {

                }
                imageTool.ImageToolResults.Add(imageResultLine);

            }
        }
        private double MakeAngleToAngle180(double row1, double column1, double row2, double column2)
        {
            double dAngle = 0;
            if (Math.Abs((column2 - column1)) < 0.000001)
            {
                if ((row1 - row2) > 0)
                    dAngle = 90.0;
                else
                    dAngle = 270.0;

            }
            else
            {
                dAngle = Math.Atan2((row2 - row1), (column2 - column1));
                dAngle = HuToAngle(dAngle);
            }
            //让角度变成+/-180度
            double dAngleTemp = dAngle;
            int iAngleTemp = (int)(dAngleTemp * 1000.0);
            iAngleTemp = iAngleTemp % 360000;
            dAngleTemp = (double)iAngleTemp / 1000.0;
            if (dAngleTemp > 180)
            {
                dAngle = dAngleTemp - 360.0;
            }
            else
            {
                if (dAngleTemp < -180)
                {
                    dAngle = dAngleTemp + 360.0;
                }
                else
                {
                    dAngle = dAngleTemp;
                }
            }
            return dAngle;
        }
        private double HuToAngle(double dHu)
        {
            return dHu / Math.PI * 180.0;
        }
        private double AngleToHu(double dAngle)
        {
            return dAngle / 180.0 * Math.PI;
        }
    }
}
